/*
 * {EasyGank}  Copyright (C) {2015}  {CaMnter}
 *
 * This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
 * This is free software, and you are welcome to redistribute it
 * under certain conditions; type `show c' for details.
 *
 * The hypothetical commands `show w' and `show c' should show the appropriate
 * parts of the General Public License.  Of course, your program's commands
 * might be different; for a GUI interface, you would use an "about box".
 *
 * You should also get your employer (if you work as a programmer) or school,
 * if any, to sign a "copyright disclaimer" for the program, if necessary.
 * For more information on this, and how to apply and follow the GNU GPL, see
 * <http://www.gnu.org/licenses/>.
 *
 * The GNU General Public License does not permit incorporating your program
 * into proprietary programs.  If your program is a subroutine library, you
 * may consider it more useful to permit linking proprietary applications with
 * the library.  If this is what you want to do, use the GNU Lesser General
 * Public License instead of this License.  But first, please read
 * <http://www.gnu.org/philosophy/why-not-lgpl.html>.
 */

package com.dhwl.hsh.merchant.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dhwl.hsh.lib.utils.SystemBarTintManager;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.ui.activity.MainActivity;
import com.dhwl.hsh.merchant.ui.dialog.LoadingDialogFragment;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * desc
 *
 * @author Derek.Kidd
 * @time 2016/4/15 10:10
 */
public abstract class BaseFragment extends Fragment {

    protected View self;

    /**
     * topbar title
     */
    public TextView topbarTitleTv;

    /**
     * topbar 右侧内容
     */
    public TextView topbarrightTv;

    private DialogFragment newFragment;


    /**
     * @param inflater           The LayoutInflater object that can be used to inflate
     *                           any views in the fragment,
     * @param container          If non-null, this is the parent view that the fragment's
     *                           UI should be attached to.  The fragment should not add the view itself,
     *                           but this can be used to generate the LayoutParams of the view.
     * @param savedInstanceState If non-null, this fragment is being re-constructed
     *                           from a previous saved state as given here.
     * @return Return the View for the fragment's UI, or null.
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        ((MainActivity) getActivity()).mTintManager.setStatusBarTintColor(getActivity().getResources()
                .getColor(R.color.color_32365d));
        (getActivity().findViewById(R.id.rootview)).setFitsSystemWindows(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            SystemBarTintManager.SystemBarConfig config = ((MainActivity) getActivity()).mTintManager.getConfig();
            (getActivity().findViewById(R.id.rootview)).setPadding(0,
                    config.getPixelInsetTop(false),
                    0,
                    config.getPixelInsetBottom());
        }

        if (null != this.self) {
            //因为共用一个Fragment视图，所以当前这个视图已被加载到Activity中，必须先清除后再加入Activity
            if (this.self.getParent() != null && this.self instanceof ViewGroup) {
                ViewGroup parent = (ViewGroup) this.self.getParent();
                parent.removeView(this.self);
            }
            return this.self;
        }
        this.self = inflater.inflate(this.getLayoutId(), container, false);
        ButterKnife.bind(this, this.self);

        topbarTitleTv = (TextView) self.findViewById(R.id.topbar_title_tv);
        topbarrightTv = (TextView) self.findViewById(R.id.topbar_right_tv);
        if (null != topbarTitleTv) {
            topbarTitleTv.setFocusable(true);
            topbarTitleTv.requestFocus();
        }

        this.initViews(this.self, savedInstanceState);
        this.initData();

        return this.self;
    }

    /**
     * 设置topbar title
     *
     * @param resId 通过string.xml资源来设置标题
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setTitleContent(int resId) {
        return setTitleContent(getString(resId));
    }

    /**
     * 设置topbar title
     *
     * @param title 通过string.xml资源来设置标题
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setTitleContent(String title) {
        topbarTitleTv.setText(title);
        return topbarTitleTv;
    }


    /**
     * Fill in layout id
     *
     * @return layout id
     */
    protected abstract int getLayoutId();

    /**
     * Initialize the view in the layout
     *
     * @param self               self
     * @param savedInstanceState savedInstanceState
     */
    protected abstract void initViews(View self, Bundle savedInstanceState);


    /**
     * Initialize the Activity data
     */
    protected abstract void initData();


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this.self);
    }

    /*********
     * Toast *
     *********/

    public void showToast(String msg) {
        this.showToast(msg, Toast.LENGTH_SHORT);
    }


    public void showToast(String msg, int duration) {
        if (msg == null) return;
        if (duration == Toast.LENGTH_SHORT || duration == Toast.LENGTH_LONG) {
            ToastUtils.show(this.getActivity(), msg, duration);
        } else {
            ToastUtils.show(this.getActivity(), msg, ToastUtils.LENGTH_SHORT);
        }
    }


    public void showToast(int resId) {
        this.showToast(resId, Toast.LENGTH_SHORT);
    }


    public void showToast(int resId, int duration) {
        if (duration == Toast.LENGTH_SHORT || duration == Toast.LENGTH_LONG) {
            ToastUtils.show(this.getActivity(), resId, duration);
        } else {
            ToastUtils.show(this.getActivity(), resId, ToastUtils.LENGTH_SHORT);
        }
    }

    /*********
     * Toast *
     *********/

//    /**
//     * 显示加载动画
//     */
//    public void showWaitingDialog() {
//        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
//        Fragment loadFragment = getActivity().getSupportFragmentManager().findFragmentByTag("load_dialog");
//        if (loadFragment != null) {
//            ft.remove(loadFragment);
//        }
//        ft.addToBackStack(null);
//
//        // Create and show the dialog.
//        newFragment = LoadingDialogFragment.newInstance();
//        newFragment.show(ft, "load_dialog");
//    }
//
//    /**
//     * 隐藏加载动画
//     */
//    public void hideWaitingDialog() {
//        if (null != newFragment) {
//            newFragment.dismissAllowingStateLoss();
//        }
//    }

}
